package com.bootdo.common.utils;


import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.BucketInfo;

public class OssUtil {
    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAIMpmv0sUCSDRh";
    private static String accessKeySecret = "zss8ECMr5NkrhoCOiav6ZtmQGDdudd";  
    private static String bucketName = "suyongpic";
    
    /**
     * 判断是否存在bucketName
     * @return
     */
    private static boolean hasBucket(OSSClient ossClient){
        return ossClient.doesBucketExist(bucketName);
    }
    
    /**
     * 创建bucket实例
     */
    private static void creatBucket(OSSClient ossClient){
        if(!hasBucket(ossClient))
            ossClient.createBucket(bucketName);
    }
    
    
    
    /**
     * 获取ossClient
     * @return
     */
    public static OSSClient ossClientInitialization(){
        return new OSSClient(endpoint, accessKeyId, accessKeySecret);
    }
    
    /**
     * 释放ossClient资源
     */
    private static void ossClientRelease(OSSClient ossClient){
        ossClient.shutdown();
    }
    
    /**
     * 获取bucket信息
     */
    @SuppressWarnings("unused")
	private BucketInfo getBucketInfo(OSSClient ossClient){
        return ossClient.getBucketInfo(bucketName);
    }
    
    
    @SuppressWarnings("unused")
	public  static void deleteBucketInfo(String objectName){

        // 删除Object。
    	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteObject(bucketName, objectName);
        // 关闭Client。
        ossClient.shutdown();
	
    }

    
    /**
     * 文件上传并返回文件路径
     * @param fileName
     * @return
     * @throws IOException 
     */
    public static String fileUpload(MultipartFile file) throws IOException{
        OSSClient ossClient = ossClientInitialization();
        creatBucket(ossClient);
        String fileName = getFileName(file.getOriginalFilename());
        // 上传文件流
        InputStream inputStream = file.getInputStream();
        ossClient.putObject(bucketName, fileName, inputStream);
        String url = getUrl(ossClient,fileName);
        ossClientRelease(ossClient);
        return url;
    }
    
    
    
    /**
     * 文件上传并返回文件路径
     * @param fileName
     * @return
     * @throws IOException 
     */
    public static String fileUploadByName(MultipartFile file,String bucketName) throws IOException{
        OSSClient ossClient = ossClientInitialization();
        if(!hasBucket(ossClient))
        ossClient.createBucket(bucketName);
        String fileName = getFileName(file.getOriginalFilename());
        // 上传文件流
        InputStream inputStream = file.getInputStream();
        ossClient.putObject(bucketName, fileName, inputStream);
        String url = getUrlByMv(ossClient,fileName,bucketName);
        ossClientRelease(ossClient);
        return url;
    }
    
    /**
     * 获取附件上传保存到服务器的名称
     * @param fileName    文件原始名称
     * @return
     */
    public static String getFileName(String fileName){
        if(StringUtils.isBlank(fileName)){
            return null;
        }
        if(fileName.lastIndexOf(".")>-1){
            fileName = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        }else{
            fileName = "."+fileName.toLowerCase();
        }
            Date date = new Date();
            long t = date.getTime();
            date = null;
        return String.valueOf(t)+fileName;
    }
    
    
    /** 
     * 获得url链接 
     * 
     * @param fileName 
     * @return 
     */  
    public static String getUrl(OSSClient ossClient,String fileName) {  
        // 设置URL过期时间为10年 3600l* 1000*24*365*10  
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);  
        // 生成URL  
        URL url = ossClient.generatePresignedUrl(bucketName, fileName, expiration);  
        if (url != null) {  
            return url.toString();  
        }  
        return null;  
    }  
    
    
    public static String getUrlByMv(OSSClient ossClient,String fileName,String bucketName) {  
        // 设置URL过期时间为10年 3600l* 1000*24*365*10  
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);  
        // 生成URL  
        URL url = ossClient.generatePresignedUrl(bucketName, fileName, expiration);  
        if (url != null) {  
            return url.toString();  
        }  
        return null;  
    }
    
  /*  public static void main(String[] args) {
    String img=	"http://suyongapp.oss-cn-beijing.aliyuncs.com/1528784948917.jpg?Expires=1844144950&OSSAccessKeyId=LTAIOp3OUSc0LaCD&Signature=oAFoKdQJypHJULvMGen7zXLxuwI%3D";
    String substring = img.substring(45, 62);
    System.out.println(substring);
    
	}*/
}